[英]Add the value of the hidden input field of other option
I ended by this code after helping with many thanks of others here in this website. 在获得本网站其他人的感谢后,我以这段代码结束。 Now, in the following code of submitting the form, I want the value of the hidden input field to be instead of Other when shown the result.
现在,在下面的提交表单的代码中,当显示结果时,我希望隐藏的输入字段的值代替Other。
<form action="" method="post">
<div class="row">
<div class="col-sm-8 col-sm-offs et-2">
<select class="form-control" name="country" id="country" required >
<option value="">Please select your country</option>
<option value="A">Country A</option>
<option value="B">Country B</option>
<option value="C">Country C</option>
<option value="Other">Other</option>
</select>
<input type ="text" id="country_other" style="display:none">
<button type="submit" class="btn btn-gpsingh">Next</a>
</div>
</div>
</form>
<script>
$("#country").change(function(){
var value = this.value;
if(value =='Other')
{
$("#country_other").show();
$("#country_other").attr('required', false);
}
else
{
$("#country_other").hide();
$("#country_other").val('');
$("#country_other").attr('required', false);
}
});
</script>
Something you could try, if I understood correctly, would be to remove the name
attribute from the SELECT
menu if the chosen value is other
and assign the name
attribute instead to the text
input element - when the form is submitted the item in the POST array with the name of country
would come from the text field and not the select menu... 如果我正确理解,您可以尝试从
SELECT
菜单中删除name
属性,如果选择的值是other
值,然后将name
属性分配给text
输入元素-当表单提交到POST数组中时, country
名称将来自文本字段,而不是选择菜单...
$('#country').change(function(){
if( this.value == 'Other' ){
$('#country_other').show();
$('#country_other').attr('required', false);
/* remove name from select & add to text field */
$('#country_other').attr('name', $(this).attr('name') );
$(this).removeAttr('name');
} else {
$('#country_other').hide();
$('#country_other').val('');
$('#country_other').attr('required', false);
/* remove name attribute from text field and assign back to select menu */
$(this).attr('name', $('#country_other').attr('name') );
$('#country_other').removeAttr('name');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.