-1

私はチップ計算機を作成しており、すべて正常に動作しますが、別の評価が選択されている場合、jQuery は式を変更しません。例:悪い、良い、素晴らしい..

jsFiddle デモ:: http://jsfiddle.net/MatthewKosloski/g7vAQ/1/

        $('#tip').on('click', function(){

            if($('#poor').click && $('#amount').val().length){

                var theAmountVal = $('#amount').val();

                    alert(theAmountVal * .10);

            } else if($('#good').click && $('#amount').val().length){

                var theAmountVal = $('#amount').val();

                    alert(theAmountVal * .15);

            } else if($('#great').click && $('#amount').val().length){

                var theAmountVal = $('#amount').val();

                    alert(theAmountVal * .20);

            } else {

                    alert("An error has occured.");

            }
4

3 に答える 3

3

それは実際にはどのように機能するのではありません.click...

.clickクリックをトリガーする関数、またはイベント バインディングを追加する関数です。

この条件を試す必要があります (x3 を他の要素に適用):

if($('#poor').hasClass('active-state')&& $('#amount').val().length)

.hasClassクラスがあるかどうかを確認し、true または false を返します。

のドキュメントは次の.hasClass()とおりです。 http://api.jquery.com/hasClass/

また、アドバイスがあれば、次のようなカスタム属性に要素を追加できます。

<h1 id="poor" data-factor='.10' data-clicked="false">Poor</h1>
<h1 id="good" data-factor='.15' data-clicked="false">Good</h1>
<h1 id="great" data-factor='.20' data-clicked="false">Great</h1>

コードを次のように減らします。

$('h1').on('click', function () {
    $(this).addClass('active-state').siblings('h1').removeClass('active-state');
});

$('#tip').on('click', function(){
    if($('.active-state').length && $('#amount').val().length){
        var theAmountVal = $('#amount').val();
        alert(theAmountVal * $('.active-state').data('factor'));
    }

});

フィドル: http://jsfiddle.net/g7vAQ/5/

于 2013-07-14T20:54:47.753 に答える
3

ここに提案があります:
デモはこちら

var choice;
$('h1').on('click', function () {
    choice = this.id;
    $(this).addClass('active-state').siblings('h1').removeClass('active-state');
});
$('#tip').on('click', function () {
    if (choice == 'poor' && $('#amount').val().length) {
        var theAmountVal = $('#amount').val();
        alert(theAmountVal * .10);
    } else if (choice == 'good' && $('#amount').val().length) {
        var theAmountVal = $('#amount').val();
            alert(theAmountVal * .15);
    } else if (choice == 'great' && $('#amount').val().length) {
            var theAmountVal = $('#amount').val();
        alert(theAmountVal * .20);
    } else {
        alert("An error has occured.");
    }
});

idクリックされたオプションのを取得してに保存した後、コードを使用してステートメントvariableを再確認しました。idif

于 2013-07-14T20:55:20.683 に答える
-2

これを試して:

jQuery(document).ready(function () {
var theAmountVal=0;

        jQuery('#tip').onclick(function(){

            if($('#poor').click && $('#amount').val().length){

                theAmountVal = $('#amount').val();

                    alert(parseInt(theAmountVal)* .10);

            } else if($('#good').click && $('#amount').val().length){

                theAmountVal = $('#amount').val();

                    alert(parseInt(theAmountVal)* .15);

            } else if($('#great').click && $('#amount').val().length){

                theAmountVal = $('#amount').val();

                    alert(parseInt(theAmountVal)* .20);

            } else {

                    alert("An error has occured.");

            }

});

そして、この From コードを削除$('#good').clickして、別のものに置き換えます。.Click - はイベントであり、IF..ELSE 構造では使用できません

于 2013-07-14T20:52:16.097 に答える