0

我正在编写一个 javascript 库,我需要制作一个 javascript 函数,您可以调用它来在您的网页上创建一个表单(调度程序)。

这是表单的 html 代码:

    <form>
    <table border="1" cellpadding="5">
    <tr><td id="schedulename" colspan="10">Time Schedule</td></tr>
    <tr>
        <td width="50">hours</td>
        <td></td>
        <td width="50">minutes</td>
        <td>Mon</td>
        <td>Tue</td>
        <td>Wed</td>
        <td>Thu</td>
        <td>Fri</td>
        <td>Sat</td>
        <td>Sun</td>
    </tr>
    <tr>
        <td width="50"><input type="text" id="hours" maxlength="2" size="1"></td> 
        <td width="2"> : </td> 
        <td width="50"><input type="text" id="minutes" maxlength="2" size="1"></td>
        <td> <input type="checkbox" id="mon"></td>
        <td> <input type="checkbox" id="tue"></td>
        <td> <input type="checkbox" id="wed"></td>
        <td> <input type="checkbox" id="thu"></td>
        <td> <input type="checkbox" id="fri"></td>
        <td> <input type="checkbox" id="sat"></td>
        <td> <input type="checkbox" id="sun"></td>
    </tr>


    <tr>
        <td colspan="10"> <input type="button" id="setbutton" value="Set schedule"></td>
    </table>
    </form>

我对结果的想法是,用户只需要在他的网页中创建一个 div,然后在 javascript 中,使用正确的参数调用我的函数,从而显示我的预定义表单。但我还需要向“设置按钮”添加一些功能,它需要将数据写入我的 PLC(可编程逻辑控制器)中的地址

我希望函数调用看起来像什么:showScheduler(id, adres); id 是部门的 id,adres 是我需要写入的可编程逻辑控制器中的地址。

从 javascript 函数中将表单写入我的网页的最佳方法是什么?

我希望你们能理解,但如果需要,我很乐意给你更多的解释。

4

2 回答 2

3

您可以执行以下操作:

<div id="placeholder">
</div>

<script type="text/javascript">
    function createForm(divId) {
        var formHtml = "<form><table border='1' cellpadding='5'>...</table></form>";
        document.getElementById(divId).innerHTML = formHtml;
    }
</script>

例如createForm("placeholder"),当你想要的时候打电话。

于 2013-04-17T13:40:05.690 回答
1

听起来您特别希望用户只有他们的 html 和一个<script>加载到您的 js 文件中的标签。您将需要一个特定的id或自定义的 css 类来用作选择器。然后用户的 html 部分看起来就像:

<html>
...
<body>
...
<div id="customID"></div>
...
</body>
</html>

然后在您的 javascript 文件中,您需要创建要写入内容的函数,并将该函数与window.load(). 就像是:

$( //note, this causes execution when the dom is loaded, using jQuery's .ready()
   function(){ showScheduler("customID"); }
);

function showScheduler(id)
{
    var contents = '<form><table border="1" cellpadding="5">...</table></form>'; //the ... needs to have the rest of your html with no single quotes anywhere inside
    $("#"+id).html(contents);
}

我不确定您正在寻找的关于该adress部分的确切内容,但您可以轻松扩展该showScheduler()函数以包含该adress值的第二个参数,然后将其粘贴到其余contents值的中间。就像是:

function showScheduler(id, adress)
{
    var contents = '<form><table border="1" cellpadding="5">...' + adress + '...</table></form>'; //the ... needs to have the rest of your html with no single quotes anywhere inside
    $("#"+id).html(contents);
}

此外,如果您希望用户从他们的页面调用该函数,您可以跳过将 this 与 this 绑定到 dom 执行的部分(即$( function(){ showScheduler("customID"); } );完全删除该部分

于 2013-04-17T13:46:11.747 回答