更改选项(选择)元素的值? [英] Altering the value of an option (select) element?
问题描述
我尝试选择< select>
表单元素的选定值,并将值附加到 -R
。 (这将用于后面的正则表达式匹配)。无论如何,到目前为止,我已经尝试了以下方法:
尝试1:
var country = $('select [name = g_country \\ ['+ value +'\\]]')。val();
$('select [name = g_country \\ ['+ value +'\\]]')。find('option [value ='+ value +']')。val(country + '-R');
尝试2:
var country = $('select [name = g_country \\ ['+ value +'\\]]')。val();
$('select [name = g_country \\ ['+ value +'\\]]')。val(country +'-R');
我可以判断选择了正确的表单元素(使用 delete_x
,其中 x
是一个数字)可以正常工作,因为当 .select-delete $ c点击$ c>,但是值设置没有。底部的注释部分是我用来检查
< select>
元素后值更改的值(或者应该是后值更改)。
下面是我的jsFiddle的链接: http://jsfiddle.net/gPF8X/11/
任何帮助/编辑/答案都将不胜感激!
最后想出了正确的选择器,道具给@gjohn这个想法。
这里是我的最终工作代码,它适当地在 g_country [x]
的结尾添加 -R
:
$('。select-delete')。click(function(){
var value = $(this) .attr('id');
value = value.replace('delete_','');
var country = $('select [name = g_country \\ [ '+ value +'\\]]')。val();
$('select [name = g_country \\ ['+ value + (国家+'-R');
$('select [name = g_country \\ ['+ value +'\\ \\\]]禁用, '1') ')ATTR(';
$('input [name = g_url\\ ['+ value +'\\]]')。attr('disabled','1');
$(this).css('display','none');
});
I'm attempting to select the selected value of a <select>
form element, and append the value with -R
. (This will be for regex matching later on). Anyway, so far I've tried the following:
Attempt 1:
var country = $('select[name=g_country\\['+value+'\\]]').val();
$('select[name=g_country\\['+value+'\\]]').find('option[value=' + value +']').val(country + '-R');
Attempt 2:
var country = $('select[name=g_country\\['+value+'\\]]').val();
$('select[name=g_country\\['+value+'\\]]').val(country + '-R');
I can tell that the selection of the correct form element (using delete_x
, where x
is a number) works fine, as the form elements to disable when .select-delete
is clicked, however the value setting doesn't. The commented portion down the bottom is what I've been using to check the value of the <select>
element post-value change (or what should be post-value change).
Here's a link to my jsFiddle: http://jsfiddle.net/gPF8X/11/
Any help/edits/answers will be greatly appreciated!
Finally came up with the right selector, props to @gjohn for the idea.
Here's my final working code, that appropriately adds -R
to the end of g_country[x]
:
$('.select-delete').click( function() {
var value = $(this).attr('id');
value = value.replace('delete_', '');
var country = $('select[name=g_country\\['+value+'\\]]').val();
$('select[name=g_country\\['+value+'\\]] > option:selected').val(country + '-R');
$('select[name=g_country\\['+value+'\\]]').attr('disabled','1');
$('input[name=g_url\\['+value+'\\]]').attr('disabled','1');
$(this).css('display', 'none');
});
这篇关于更改选项(选择)元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!