2016-08-06 27 views
0

選択(テキストボックス名)と数量(テキストボックス名)が等しい場合、どのようにしてテキストボックスを無効にすることができますか?私はこのコードを使用していましたが、選択(テキストボックス名)が3に等しい場合、txtCombo(テキストボックス名)は無効になっていませんが、4になると無効になりました。前もって感謝します。2つのテキストボックスが等しい場合にテキストボックスを無効にする

// disable button if equal 
 
// $('#button').click(function(){ 
 
$('#button').click(function() { 
 
    var firstValue = $("#quantitytotransfer").val(); 
 
    var secondValue = $("#selected").val(); 
 
    if ((firstValue == secondValue)) { 
 
    $("#txtCombo").prop("disabled", true); 
 
    } 
 
}); 
 

 
function addCombo() { 
 

 
    var textb = document.getElementById("txtCombo"); 
 
    var combo = document.getElementById("combo"); 
 
    var option = document.createElement("option"); 
 
    option.text = textb.value.trim(); 
 
    option.value = textb.value.trim(); 
 
    option.selected = true; 
 

 
    if ($('#combo option[value="' + textb.value.trim() + '"]').text() == textb.value.trim()) { 
 
    alert("Duplicate found or you entered empty value"); 
 
    return false; 
 
    } 
 
    try { 
 
    combo.add(option, null); //Standard 
 
    } catch (error) { 
 
    combo.add(option); // IE only 
 
    } 
 
    textb.value = ""; 
 
} 
 

 

 

 
$("#txtCombo").on("keydown", function(e) { 
 
    return e.which !== 32; 
 
}); 
 

 

 

 

 
$(document).ready(function() { 
 

 
    $('#button').click(function() { 
 

 
    var data = []; 
 
    $.each($("#combo option:selected"), function() { 
 
     data.push($(this).attr("value")); 
 

 
    }); 
 
    $('#imei').val(data.join(","));; 
 
    var count = $("#combo :selected").length; 
 
    $('#selected').val(count); 
 

 

 
    }); 
 

 
}); 
 

 

 
$(document).ready(function() { 
 

 
    $('#button').click(function() { 
 

 
    var data = []; 
 
    $.each($("#combo option:selected"), function() { 
 
     data.push($(this).attr("value")); 
 

 
    }); 
 
    $('#imei').val(data.join(","));; 
 
    var count = $("#combo :selected").length; 
 
    $('#selected').val(count); 
 

 

 
    }); 
 

 
}); 
 

 

 
$("#combo").on('change', function() { 
 

 
    var count = $("#combo :selected").length; 
 
    $('#selected').val(count); 
 

 

 
}); 
 
var text = $("#text").val(); 
 

 
var previousOption; 
 
$('select[name=combo] option').each(function() { 
 
    if (this.text == previousOption) $(this).remove(); 
 
    previousOption = this.text; 
 
}); 
 

 

 

 
// separated by comma to textbox 
 
$(document).ready(function() { 
 

 
    $("#combo").change(function() { 
 
    var data = []; 
 
    $.each($("#combo option:selected"), function() { 
 
     data.push($(this).attr("value")); 
 
    }); 
 
    $('#imei').val(data.join(","));; 
 

 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<BODY style="font-family: sans-serif"> 
 

 
    <fieldset> 
 
    <legend>Combo box</legend> 
 
    Add to Combo: 
 
    <input type="text" name="txtCombo" id="txtCombo" />Selected: 
 
    <input type="text" name="selected" id="selected" />IMEI Selected: 
 
    <input type="text" name="imei" id="imei" />Quantity: 
 
    <input type="text" name="quantity" value="3" id="quantitytotransfer" /> 
 
    <br> 
 

 
    <input type="button" id="button" value="Add" onclick="addCombo()"> 
 
    <br/>Combobox: 
 
    <select name="combo" multiple id="combo"></select> 
 
    </fieldset> 
 
</BODY>

+0

ワウ!あなたのhtmlは本当に台無しですか?あなたはこのフォームで正確に何をしたいですか? – jonju

+0

転送する数量が等しいとtxtComboを無効にする必要があります –

答えて

0

あなたは簡単にこれを達成することができます

function addCombo() { 
 
    var txtSelected = $('#selected').val(); 
 
    var qty = $('#quantitytotransfer').val(); 
 
    if(parseInt(txtSelected) == parseInt(qty)) { 
 
    alert('Equal'); 
 
    document.getElementById('txtCombo').disabled = true; 
 
    } else { 
 
    alert('Not equal'); 
 
    document.getElementById('txtCombo').disabled = false; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<BODY style="font-family: sans-serif"> 
 
    <fieldset> 
 
     <legend>Combo box</legend> 
 
     Add to Combo: <input type="text" name="txtCombo" id="txtCombo"/><br> 
 
     Selected: <input type="text" name="selected" id="selected" class="toggle"/><br> 
 
     IMEI Selected: <input type="text" name="imei" id="imei"/><br> 
 
     Quantity: <input type="text" name="quantity" value="3" id="quantitytotransfer" class="toggle"/><br> 
 
     <input type="button" id="button" value="Add" onclick="addCombo()"><br> 
 
     Combobox: <select name="combo" multiple id="combo"></select> 
 
    </fieldset> 
 
</BODY>

function checkIfEqual(){ 
    if($("#selected").val() === $("#quantitytotransfer").val()) 
     $("#txtCombo").prop("disabled", true); 
    else 
     $("#txtCombo").prop("disabled", false); 
} 

$("#button").click(function(){ 
    $("#selected").val($("#combo option:selected").length); 
    checkIfEqual(); 
}); 

demo here

+0

コンボボックスで選択したコンボボックスごとに自動生成された値が入力されませんでした –

+0

わかりませんが、私の編集をご覧ください –

+0

stil not working –

0

をあなたはこれを試してみました

+0

その1つの値を追加 –

関連する問題