我有几个带有文本输入的div和几个带有无线电类型输入的div。我尝试使用jquery在单击按钮时将这些输入的值分配给某些变量,但它只是t seem to work (
echo $ number_value or
echo $ second_form_value`什么都没有显示)
输入部分:
<div id="first_div">
<form name="first_form" action="">
<fieldset>
<label for="first_form" id="first_form_label">Some text for the user</label>
<input type="number" name="number_info" id="number_info" size="7" value="" class="text-input" />
<br />
<label class="error" for="first_form" id="first_form_error">Error alert</label>
<br />
<input type="button" name="value" class="button" id="first_form_button" value="Next" />
</fieldset>
</form>
</div>
<div id="second_div">
<form name="second_form" action="">
<fieldset>
<label for="second_form" id="second_form_label">Some text for the user</label><br />
<input type="radio" name="second_form_info" id="second_form_A" value="A" class="text-input" />
<label for="A">Choose A</label><br />
<input type="radio" name="second_form_info" id="second_form_B" value="B" class="text-input" />
<label for="B">Choose B</label>
<br />
<label class="error" for="second_form" id="second_form_error">Error alert</label>
<br />
<input type="button" name="value" class="button" id="second_form_button" value="Next" />
</fieldset>
</form>
</div>
和jquery部分:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
(文档)$。就绪(函数(){
$('#first_form_button').click(function(){
$("#number_info").on("keyup change", function() {
var number_value = $('#number_info').val();
});
$('#first_div').hide();
$('#second_div').show();
});
$('#second_form_button').click(function(){
$("#second_form_info").on("keyup change", function() {
var second_form_value = $('#input[name$='second_form_info']').val();
});
$('#first_div').hide();
});
有人可以指出其中存在根本缺陷吗?
答案 0 :(得分:0)
您正在等待keyup
和change
的处理程序绑定,直到您单击表单中的按钮为止。将这些处理程序放在click
函数之外。您在.change
处理程序中设置的变量也是该处理程序的本地变量,因此无法在外部访问它。
$("#second_form_info").on("keyup change", function() {
var second_form_value = $('#input[name$='second_form_info']').val();
});
$('#second_form_button').click(function(){
$('#first_div').hide();
});