我制作了许多只包含单选按钮和提交按钮的html表单。 但是,我无法让他们正确验证 - 彼此独立。 这是我的代码:
表单的PHP / HTML代码:
<table> <?php for($i=0; $i<count($array1); $i++) { $number = $i + 1; echo "<TR><TD>; echo "<form name='move' action='listChange_controller.php' method='POST'>Title:<br/> <input type='radio' name='change".$number."' value = 'val1' />Thing1 <input type='radio' name='change".$number."' value = 'val2'/>Thing2 <input type='radio' name='change".$number."' value = 'val3'/>Thing3 <input type='button' name='submit' value='submit' onClick='validate(".$number.");'/>"; echo "</form>"; echo "</TD></TR>"; } ?> </table>这是我一直在尝试的javascript / jquery,但没有奏效:
function validate(number) { var name_var = 'change'+number; if($('input:radio[name=name_var]:checked').length > 0) { $.post('file.php',{ name_var:$('input:radio[name=name_var]:checked').val()}); } else { alert('You must choose'); return false; } }当我这样做时,它总是认为我在按下提交之前没有选择单选按钮; 它总是执行我的javascript函数的'else'部分。
请让我知道为什么它不工作,什么会工作。 任何帮助和建议表示赞赏。
I made a number of html forms containing only radio buttons and a submit button. However, I cannot get them to validate properly--independently of each other. Here is my code:
PHP/HTML code for the form:
<table> <?php for($i=0; $i<count($array1); $i++) { $number = $i + 1; echo "<TR><TD>; echo "<form name='move' action='listChange_controller.php' method='POST'>Title:<br/> <input type='radio' name='change".$number."' value = 'val1' />Thing1 <input type='radio' name='change".$number."' value = 'val2'/>Thing2 <input type='radio' name='change".$number."' value = 'val3'/>Thing3 <input type='button' name='submit' value='submit' onClick='validate(".$number.");'/>"; echo "</form>"; echo "</TD></TR>"; } ?> </table>Here is the javascript/jquery I have been trying, but has not worked:
function validate(number) { var name_var = 'change'+number; if($('input:radio[name=name_var]:checked').length > 0) { $.post('file.php',{ name_var:$('input:radio[name=name_var]:checked').val()}); } else { alert('You must choose'); return false; } }When I do this, it always thinks I have not chosen a radio button before pressing submit; it always carries out the 'else' part of my javascript function.
Please let me know why it is not working, and what would work. Any help and suggestions appreciated.
最满意答案
这条线......
if($('input:radio[name=name_var]:checked').length > 0)应该读
if($('input:radio[name=' + name_var + ']:checked').length > 0)This line...
if($('input:radio[name=name_var]:checked').length > 0)should read
if($('input:radio[name=' + name_var + ']:checked').length > 0)更多推荐
javascript,radio,电脑培训,计算机培训,IT培训"/> <meta name="description&
发布评论