之前选择的值也会附加在textarea中,但我只想在textarea中附加一个(当前)选定值
$(document).ready(function() {
$(document).click("click", function(e) {
if ((e.target.value != undefined) && (e.target.id == "checkit")) {
if ((e.target.id != "if") && (e.target.id != "while")) {
$('#sms_text').val($('#sms_text').val() + e.target.value + '\n');
}
}
});
// second
$('#mc_nos').change(function() {
if ($('#mc_nos option:selected')) {
$("#mc_nos option:selected").addClass("important");
}
});
$(document).change("change", function(e) {
if ((e.target.value != undefined) && (e.target.id == "mc_nos")) {
if ($("#mc_nos option:selected")) {
$(this).find("option:selected").each(function() {
if ($(this).attr("class") == "important") {
$('#sms_text').val(e.target.value + '\n' + $('#sms_text').val());
}
});
}
}
});
});
我想在leftside按钮(屏幕截图)和选择框选项值附加值。
选择框选项值仅附加一个当前选定的选项
答案 0 :(得分:0)
你应该知道的一些要点
1st: 使用!==
代替!=
第二名: 使用
$('#mc_nos').change(function() {
$("#mc_nos option").removeClass("important");
$("#mc_nos option:selected").addClass("important");
});
而不是
$('#mc_nos').change(function() {
if($('#mc_nos option:selected')){
$("#mc_nos option:selected").addClass("important");
}
});
第三名: 如果您需要检查
if($('#mc_nos option:selected'))
使用
if($('#mc_nos option').is(':selected'))
// or
if($('#mc_nos option:selected').length > 0)
第四次 检查课程
if($(this).attr("class")=="important")
改为使用
if($(this).hasClass("important")) // if has class
// or
if(!$(this).hasClass("important")) // if not
关于追加点的第五: ..例如,您可以使用隐藏范围 ..说你有一个跨度
<span class="saveselected" style="display:none;"></span>
当您附加所选数据时,请捕获跨前的文本,然后将textarea值中的范围文本替换为&#39; nothing&#39;然后将新文本追加到另一个......这样的事情
if($('span.saveselected').text().trim() !== ''){ // if span not empty
var span_previous_text = $('span.saveselected').text(); // get span previous text
var textarea_previous_value = $('#sms_text').val(); // get textarea current value
var new_textarea_value = textarea_previous_value.replace(span_previous_text ,''); // replace the span text in textarea with ''
$('#sms_text').val(new_textarea_value); // all data again without the previous selected data
}
答案 1 :(得分:0)
您好,因为我不知道您的HTML是怎样的,我只是解决您的问题,并创建了一个演示..请检查https://jsfiddle.net/hscrhokc/2/
<强>已更新强> https://jsfiddle.net/hscrhokc/7/
<强> HTML 强>
<select class="dd">
<option>1111111111</option>
<option>6666666666</option>
<option>7777777777</option>
<option>8888888888</option>
<option>9999999999</option>
</select>
<div class="menu">
<span>some text1</span>
<span>some text2</span>
<span>some text3</span>
<span>some text4</span>
</div>
<br>
<textarea class="ta">
fdsfdsf
ewqew
bnmnmnm
</textarea>
<强>的jQuery 强>
$(document).ready(function(){
$('.menu').on('click', 'span', function(){
$('.ta').prepend($(this).text()+'\n');
});
$('.dd').on('change', function(){
var taVal= $('.ta').text(); // store textara text in a variable
var taArr=taVal.split("\n"); // split string into words and store it as an array taArr
// traverse throug the dropdown
$('.dd option').each(function(i){
// check whether the dropdown value exists in textarea text
if($.inArray(this.value,taArr)!=-1){
$('.ta').text(taVal.replace(this.value+'\n', '')); // if found remove it
}
});
//prepend the selected dropdown value to textarea
$('.ta').prepend($('.dd').val()+'\n');
});
});