2016-07-08 15 views
1

Diese Jquery-Anweisung hat 2 Funktionen. Die zweite Funktion isAnswered ist, wo ich die Probleme bekomme. Es sieht irgendwie nicht, dass der Radioknopf angeklickt wird und ich bin mir nicht sicher warum (vielleicht die neuere Version von Jquery?)?Wie können Sie mithilfe von JQuery überprüfen, ob ein Optionsfeld angeklickt wurde?

JQUERY hat zwei Funktionen, aber zweite ist die Frage - immer falsch zurück kommt für show_action:

//[F] button clicked 
$(".upload_files").click(function() { 
    var disabled = $(this).attr("do_disable"); 
    fileClick(audit_instance_id, this, disabled); 
}); 

function fileClick(audit_instance_id, data, disabled) { 
var questionID = data.id; 
var show_action = isAnswered(questionID);// This always comes back false 
if (show_action) { 
    $('#question_id').val(questionID); 
    $('#uploaded_files_' + questionID).toggle(); 
    //problem after this 
    var visible = $('#uploaded_files_' + questionID).is(':visible'); 
    if (visible === true) { 
    var params = { 
     'action': 'get_files', 
     'audit_instance_id': audit_instance_id, 
     'question_id': questionID 
    }; 
    $.post('ajax/perform_audit.php', params, function(data) { 
     displayFiles(data, questionID, audit_instance_id, disabled); 
    }); 
    show_action = false; 
    } 
} else { 
    alert("You must answer the Audit question before adding a file."); 
} 
return false; 
} 


function isAnswered(comment_id) { 
var show_action = false; 
$("#options_" + comment_id + " input, #options_" + comment_id + " textarea").each(function(){ 

//This part seems to be teh issue or the .each above 

    if($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio"){ 
     if($(this).is(':checked')){ 
      show_action = true; 
     } 
    } else if($(this).attr("type") == "text" || $(this).is("textarea")){ 
     if($(this).val()){ 
      show_action = true; 
     } 
    } 
}); 
return show_action; 
} 

HTML:

<a title='Upload file' href='#!' id=$row[questionID] class='btn btn-default btn-xs upload_files'> 

<input name="4117" value="1983" id="option_1983" type="radio"> 
<input name="4117" value="1984" id="option_1984" type="radio"> 
+1

versuchen in if-Anweisung - $ (this) .prop ('checked') –

+0

Wie konnte ich sehen, wenn type = Radio aktiviert ist? –

+1

'if ($ (this) .prop ('checked')) {show_action = true;}' –

Antwort

-1

Versuchen:

if ($(this).checked){ 
} 

Sie könnten auch Versuchen Sie, eine Protokollierung für die Entwicklung hinzuzufügen, um sicherzustellen, dass die component_id eintrifft, was Sie erwarten.

+0

Ich benutzte Alarm (datafosomesort); hauptsächlich, wie hast du es log? –

+1

Ich benutze auch Alarm. Es hilft nur zu wissen, was der Wert ist. Sie können dann Ihre Browser-Entwicklungstools (F12) verwenden, um sicherzustellen, dass die für das Optionsfeld die gleiche ID hat wie das, was im Warndialog angezeigt wird. – CDurand

+0

Der Inspektor ... Ich liebe es, das zu benutzen! Gibt es da etwas, worauf man achten sollte, wie die Registerkarte "Netzwerk"? –

0

eine gründliche Möglichkeit, dies zu überprüfen, wäre es zu überprüfen, ob etwas überprüft wird, z.

$('#trigger').click(function() { 
    if (!$("input[type="radio"][name='name']:checked").val()) { 
     alert('Nothing is checked!'); 
     return false; 
    } 
    else { 
     alert('A radio button is checked!'); 
    } 
    }); 

Um das Konsolenprotokoll zu verwenden, verwenden Sie z. console.log("nothing is checked"); und dann, wenn Sie F12 drücken, werden Sie die Nachrichten und alle Fehler auch sehen.

$('#trigger').click(function() { 
 
    if (!$("input[type='radio'][name='fruit']:checked").val()) { 
 
     alert('Nothing is checked!'); 
 
     return false; 
 
    } 
 
    else { 
 
     var selVal = ""; 
 
selVal = $("input[type='radio'][name='fruit']:checked").val(); 
 
     alert('A radio button ' + selVal + ' is checked'); 
 
    } 
 
    }); 
 

 
//if we take out the [name='fruit'] bit the function will work for both input sets
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://code.jquery.com/jquery.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
</head> 
 
<body> 
 
    <input name="fruit" value="banana" id="banana" type="radio"> 
 
<label for="banana">banana</label> 
 
<input name="fruit" value="apple" id="apple" type="radio"> 
 
<label for="apple">apple</label> 
 
<input name="fruit" value="orange" id="orange" type="radio"> 
 
<label for="orange">orange</label> 
 
<br><br> 
 
    <input name="boy" value="Mark" id="Mark" type="radio"> 
 
<label for="Mark">Mark</label> 
 
<input name="boy" value="Dan" id="Dan" type="radio"> 
 
<label for="Dan">Dan</label> 
 
<input name="boy" value="Harry" id="Harry" type="radio"> 
 
<label for="Harry">Harry</label> 
 
<br><br> 
 

 
<input id="trigger" type="submit" value="Submit"> 
 
</body> 
 
</html>

+0

http://www.bootply.com/vmMkZ223j8 Ich habe deinen Code ausprobiert und kannst du mir sagen, was ich falsch mache? –

+0

Nur eine Sekunde, sah gerade msg, bin am Telefon muss Laptop aufwachen! –

+0

http://jsbin.com/rakepehoza/edit?html,js,output funktioniert in jsbin? nur Diff ist einzelne Anführungsstriche um 'Radio' .. aber Sie hatten keinen #trigger definiert, ich fügte einen als Knopf hinzu –

Verwandte Themen