我有一個表單,可以動態添加表單元素。我添加的元素是一個文本字段和一個單選按鈕。現在,我希望所選單選按鈕的值是用戶在相鄰輸入框中鍵入的文本。這裏是JS:將單選按鈕的值設置爲jquery中最接近輸入文本的文本
var $node = "";
var varCount=0;
$('body').on('click', '.removeVar', function(){
$(this).parent().remove();
varCount--;
});
$('#addfld').on('click', function(){
varCount++;
$node = '<label for="losfldlbl[]">Field Name '+varCount+': </label>' +
'<input type="text" name="losfldlbl[]" id="losfldlbl[]" title="Custom field name cannot be blank" required=true placeholder="Field name'+varCount+'"/>'+
'Is this the value field? <input type="radio" name="losvaluefld[]" id="losvaluefld[]" value=false /> '+
'<span class="removeVar label label-important"><a id="removeFld" href="#" title="Click here to remove this field"><i class="icon-minus icon-white"></i></a></span>';
$(this).parent().before($node);
});
$('input:radio').click(function() {
$(this).attr('value', $(this).prev().attr("value"));
alert($(this).val());
});
,這裏是我的HTML:
<form id='myForm'>
click on the yellow button custom fields <span class='label label-warning'><a id='addfld' href='#' title='Click here to add a new field'><i class='icon-plus icon-white'></i></a></span>
</form>
我共享了JsFiddle here。問題是我無法將從輸入文本傳遞的值傳遞給單選按鈕的值。
你爲什麼不能等到用戶完成添加元素然後抓住一切,然後呢? – jmm 2013-03-25 22:12:45