简体   繁体   English

在先前的Web表单中输入数据后,如何使Web表单出现?

[英]How to make web form appear after previous web form has data entered into it?

I'm looking to make a second web form appear after the first web form has something entered into it. 我希望在第一个Web表单中输入某些内容后使第二个Web表单出现。 I'm currently using a sinatra set up with slim as the templating engine, and pure javascript. 我目前正在使用设置为slim作为模板引擎和纯javascript的sinatra。

input#topic value="Enter topic" onfocus="this.value = this.value=='Enter topic'?'':this.value;" onblur="this.value = this.value==''?'Enter topic':this.value;"

input#subtopic value="Enter subtopic" onfocus="this.value = this.value=='Enter subtopic?'':this.value;" onblur="this.value = this.value==''?'Enter subtopic':this.value;"

The above are my two forms, the onfocus and onblur are to make the form value disappear and reappear if clicked on. 上面是我的两个表单,onfocus和onblur是使表单值消失并在单击时重新出现。

My javascript is as follows. 我的JavaScript如下。

function checkField(field) {
    if(field.value != null) {
        document.getElementById('subtopic_form').style.display = 'true';
    } else {
        document.getElementById('subtopic_form').style.display = 'false';
    }
}

This doesn't work, and part of the problem is that when I add to the input tags 这是行不通的,部分问题是当我添加到输入标签时

onchange="checkField(this)"

then I get a function unused message inside my javascript file, even though I have specified in my home.slim file 那么即使我在home.slim文件中已指定,我的javascript文件中也会收到未使用函数的消息

script src="/js/application.js"

Any help to get this to work as I need is much appreciated. 非常感谢您为我提供帮助,使其能够正常工作。 I'm open to using jquery for this, and if there was any way to make the second form have an effect upon appearance that'd be awesome. 我愿意为此使用jquery,如果有任何方法可以使第二种形式对外观产生影响,那就太好了。

-Adam -亚当

The display property accepts several values, but true and false are not among them. display属性接受几个值,但是true和false不在其中。 To get a full list, go to MDN or w3schools or MSDN . 要获取完整列表,请访问MDNw3schoolsMSDN But I can tell you right now, that the values you most likely want are "none" and "block", as in: 但是我现在可以告诉您,您最可能想要的值是“ none”和“ block”,例如:

function checkField(field) {
    if(field.value != null && field.value != '') {
        document.getElementById('subtopic_form').style.display = 'block';
    } else {
        document.getElementById('subtopic_form').style.display = 'none';
    }
}

with jQuery, though, this code could be quite a bit cleaner! 但是,使用jQuery,此代码可能会更干净!

function checkField(field) {
    if (field.value != null && field.value != '') {
        $("#subtopic_form").show();
    } else {
        $("#subtopic_form").hide();
    }
}

or even 甚至

$("#topic").change(function(){
    if ($(this).val()) {
        $("#subtopic_form").show();
    } else {
        $("#subtopic_form").hide();
    }
});

and if you want effects, consider jQuery's .fadeOut() or .slideUp() in place of .hide() 如果需要效果,请考虑使用jQuery的.fadeOut().slideUp()代替.hide ()

Note that I added field.value != '' to your code. 请注意,我在您的代码中添加了field.value!=”。 Also, it would be best to use !== instead of !=. 另外,最好使用!==代替!=。

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以Web形式传递用户输入的数据并将其传递给Java Script函数 - How to pass user entered data in a web form and pass it into a Java Script function 提交Optin表单后,如何使javascript或php SUCCESS消息出现 - How to make a javascript or php SUCCESS message appear after optin form has been submitted 如何检查用户是否尚未将数据输入表单(用于发送)? - How to check if the user has not entered the data to a form (befor sending)? 如何使计算文本在 Web 表单中可编辑 - How to make Computed Text be editable in Web Form 单击提交后如何以html形式保留输入的数据 - How to retain the entered data in an html form after clicking submit 单击按钮后如何将带有输入数据的TextField传递到表单? - How to pass TextField with entered data into form after clicking a button? 加载Web表单后加载外部JavaScript文件 - Loading an external JavaScript file after web form has loaded 选择选项后如何显示多个表单 - how to make multiple form inputs appear after option is selected 在插入表单的同一页上出现“数据输入成功”消息吗? - “Data entered successfully” message to appear on same page of insert form? 试图使下一个和上一个按钮出现在注册表的不同时间 - Trying to make the next and previous button appear at different times of the registration form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM