显示/隐藏onClick jQuery

时间:2018-07-13 03:02:27

标签: jquery css css3

单击按钮后,我想在CSS上显示/隐藏文本display: none;,并清除onClick函数的输入字段。现在,文本一旦显示就仍然显示。谢谢您的帮助。

$('#submit').click(function (e) {
    e.preventDefault();
    str = $('#string').val();

    if (checkPalin(str)) {
        return $('#true').css('display', 'block');
    } else {
        return $('#false').css('display', 'block');
    }
});
#result span {
    display: none;
    font-weight: bold;
}
<input type="text" name="string" id="string" value="check">
<input id="submit" type="submit" value="check">
<p id="result">
    <span id="true">This string is a padindrome!</span>
    <span id="false">This string is
        <strong>not</strong> a padindrome
    </span>
</p>

1 个答案:

答案 0 :(得分:0)

尝试:

$('#submit').click(function (e) {
    e.preventDefault();
    str = $('#testString').val();
    // Console logging
    console.log('The value entered was: ' + str);
    if (checkPalin(str)) {
        // Clear the value of #testString
        $('#testString').val('');
        // Display the #true result
        $('#true').show();
        // Hide the #false result
        $('#false').hide();
        // Console logging
        console.log('The value ' + str + ' passed our condition');
    } else {
        // Clear the value of #testString
        $('#testString').val('');
        // Display the #false result
        $('#false').show();
        // Hide the #true result
        $('#true').hide();
        // Console logging
        console.log('The value ' + str + ' failed our condition');
    }
    return false;
});

除了将使用Hide()和Show()方法根据需要显示结果外,基本上将像以前一样检查条件。

相关问题