javascript - 值未显示,即使它位于 HTML 中

标签 javascript jquery html forms

我有一个表单,可以根据某个字段的值显示和隐藏某些字段,执行此操作的 javascript 如下,

jQuery(function(){

    jQuery('.voice_over, .commercial').hide();

    if(jQuery("#type option:selected").val() == "voice over")
    {
        jQuery('.voice_over, .role').fadeIn(500);
        jQuery('.generic, .fieldError, .commercial').fadeOut(500).val('');
    }

    if(jQuery("#type option:selected").val() == "television" || jQuery("#type").val() == "theatre" || jQuery("#type").val() == "radio" || jQuery("#type").val() == "film" || jQuery("#type").val() == "commercial" )
    {
        jQuery('.voice_over, .commercial').fadeOut(500).val('');
        jQuery('.generic, .role').fadeIn(500);
    }

    if(jQuery("#type option:selected").val() == "commercial") {
        jQuery('input[name="production_role"], .voice_over, .generic, .role').fadeOut(500).val('');
        jQuery('.commercial').fadeIn(500);
    }

    jQuery("#type").change(function(){ 

        if(jQuery(this).val() == "voice over")
        {
            jQuery('.generic, .commercial, .fieldError').fadeOut(500).val('');
            jQuery('.voice_over').fadeIn(500);
        }
        else if(jQuery(this).val() == "commercial") {
            jQuery('input[name="production_role"], .role, .generic, .fieldError').fadeOut(500).val('');
            jQuery('.commercial').fadeIn(500);
        }
        else if(jQuery(this).val() == "television" || jQuery(this).val() == "theatre" || jQuery(this).val() == "radio" || jQuery(this).val() == "film")
        {
            jQuery('.commercial, .fieldError, .voice_over').fadeOut(500).val('');
            jQuery('.generic, .role, input[name="production_role"]').fadeIn(500);   
        }

    });
});

我得到的 HTML 如下(这是通过 PHP 生成的),

<form action="http://urbantalent.factoryagency.co.uk/admin/candidates/add_step_3/id/177/edit" method="post" accept-charset="utf-8" class="step_3">
                <label for="type">Production Type</label>
<select name="production_type" id="type">
<option value="television">Television</option>
<option value="theatre">Theatre</option>
<option value="film">Film</option>
<option value="commercial" selected="selected">Commercial</option>
<option value="voice over">Voice Over</option>
<option value="short film">Short Film</option>

<option value="corporate">Corporate/Training</option>
<option value="radio">Radio</option>
</select>
                <label style="display: none;" class="generic">Production Title</label>
        <input style="display: none;" name="production_title" value="" class="generic" type="text">
                <label style="display: none;" class="voice_over">Project Type</label>
        <input style="display: none;" name="production_project_type" value="" class="voice_over" type="text">
                <label style="display: none;" class="voice_over">Name/Product</label>

        <input style="display: none;" name="production_product" value="" class="voice_over" type="text">
                <label style="display: none;" class="voice_over">Production Agency</label>
        <input style="display: none;" name="production_agency" value="" class="voice_over" type="text">
                <label style="display: none;" class="role">Role</label>
        <input style="display: none;" name="production_role" value="" type="text">
                <label style="display: none;" class="generic">Director</label>
        <input style="display: none;" name="production_director" value="" class="generic" type="text">

                <label style="display: none;" class="generic">Production Company</label>
        <input style="display: none;" name="production_company" value="" class="generic" type="text">
                <label style="display: block;" class="commercial">Commercial Details</label>
        <input style="display: inline;" name="production_details" value="Walking down the street looking happy - Daz (2010)" class="commercial" type="text">
                <input name="career_overview_id" value="177" type="hidden">
                <input name="candidate_id" value="" type="hidden">
                <input name="form_step" value="3" type="hidden">
                    <input name="step_3" value="Save" type="submit">

            </form>

如您所见,product_details 字段有一个值,但我在该字段中看不到任何内容。

当我删除 JavaScript 时,我看到该值很好,所以我只能假设其中存在错误,但我看不到它。

最佳答案

if(jQuery("#type option:selected").val() == "television" || jQuery("#type").val() == "theatre" || jQuery("#type").val() == "radio" || jQuery("#type").val() == "film" || jQuery("#type").val() == "commercial" )
    {
        jQuery('.voice_over, .commercial').fadeOut(500).val('');
        jQuery('.generic, .role').fadeIn(500);
    }

您正在将 val 设置为空字符串。

重要的部分是:

if(... jQuery("#type").val() == "commercial"){
    jQuery('..., .commercial')...val('');
}

关于javascript - 值未显示,即使它位于 HTML 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8562493/

相关文章:

jquery - 使用 jQuery 模拟按下 Tab 键

javascript - 自动选择当前类 - 导航菜单

javascript - 如何在 clearInterval() 之后启动一个 eggtimer?

javascript - 加载事件是否会等待加载 css 中的背景图像?

javascript - Wordpress 中的内联 Javascript

javascript - 单击鼠标悬停以应用样式

javascript - 如何使元素始终位于结束标记之上

javascript - Service Worker - 使用 skipWaiting() 在新版本上更新缓存

php - Jquery slider 面板闪烁

html - CSS Speech bubble created with CSS 的线条粗细问题