简体   繁体   English

动态生成Javascript提交按钮

[英]Dynamically generate Javascript submit button

First of all, please let me know if I need to phrase this better to make more sense, and thanks for reading. 首先,请让我知道是否需要更好地表述以便更有意义,并感谢您的阅读。

I am currently working on a webpage that creates a large number of form elements where the id, name and value of each form element depends on some previous user selection, as in the following example, where the id, name and value depend on whatever value $arr contains at the time, 我目前正在制作一个创建大量表单元素的网页,其中每个表单元素的id,名称和值取决于以前的用户选择,如以下示例所示,其中id,名称和值取决于任何值$ arr当时包含

?>
<div id='<?= $arr['param_name'] ?>Div' class='dynamicDiv'>
    <b><?= $arr['param_desc'] ?></b>
    <br/>
    <input type='Text' id='<?= $arr['param_name'] ?>' 
        name='<?= $arr['param_name'] ?>'
        value='<?= $arr['default_val'] ?>' class='dynamicTextBox'>
</div>
<?php

That part is largely fine, but then there is a number of submit buttons that are similarly generated dynamically, but are hard-coded. 那部分基本上没问题,但是然后有许多提交按钮,这些按钮类似地动态生成,但是经过硬编码。 The jQuery for these input buttons is hard-coded, as in the following example, 这些输入按钮的jQuery是硬编码的,如以下示例所示,

<input type='Submit' value=' Query Data' name='funcSearch' 
    onclick="queryAngle(('<?php echo $result[0]['query_id'] ?>'),('<?php echo $_GET['schema'] ?>'),
    ($('#firstFrame').val()), ($('#lastFrame').val()),($('#skip').val()), ($('#atomID1').val()),  
    ($('#atomID2').val()), ($('#atomID3').val())); false;" class='dynamicParamButton'><br/><br/>

Being that there are a dozen or so of these submit buttons, each hard-coded, and most at least twice the size of this one here, it gets pretty complicated pretty fast. 由于这些提交按钮有十几个左右,每个都经过硬编码,并且至少是此按钮的大小的两倍,因此它变得非常复杂。 I would like to take some sort of object-oriented approach to make this thing maintainable, but need some help figuring out on a high level where to start. 我想采用某种面向对象的方法来使此东西可维护,但是需要一些帮助来确定从哪里开始。 Is there a good place to start? 有一个好的起点吗? Maybe I should generate a submit button as a string in PHP, or there is some jQuery functionality to address this issue? 也许我应该在PHP中将提交按钮生成为字符串,或者有一些jQuery功能可以解决此问题?

For completeness, the submit button in the example pasted above sends the values to the following function in a javascript file, 为了完整起见,上面粘贴的示例中的Submit按钮将值发送到javascript文件中的以下函数,

function queryAngle(query_id, schema, firstFrame, lastFrame, frameSkip, atomID1, atomID2, atomID3) {
if (query_id === "") {
    $("#QueryInfo").html("<p>Error Please enter valid arguments</p>");
    return;
}
$("#waiting").show(500);
$("#interface").hide(0);

$.post('./function_files/angleFunction.php',
    {
        query_id: query_id,
        simschema: schema,
        firstFrame: firstFrame,
        lastFrame: lastFrame,
        frameSkip: frameSkip,
        atomID1: atomID1,
        atomID2: atomID2,
        atomID3: atomID3
    },

    function (data) {
        showResultReady(data);
    });
}

But there are a dozen or so of these functions too, like queryAngle shown here, but also queryDensity, queryTorsion, and so on. 但是这些函数也有十几个,例如这里显示的queryAngle,还有queryDensity,queryTorsion等等。 They all do essentially the same thing - pass the values on to a file, angleFunction.php, densityFunction.php, torsionFunction.php, respectively, and it would be great to have a way to bring some abstraction or object-oriented perspective to this. 它们基本上都做相同的事情-将值分别传递到文件angleFunction.php,densityFunction.php,torsionFunction.php,并且很高兴有一种方法可以对此带来一些抽象或面向对象的观点。 Thanks for any help or ideas. 感谢您的帮助或想法。

In my experience there isn't any super easy and clean way to generate JavaScript with PHP dynamically, you usually end up with ugly code here or there. 以我的经验,没有任何一种超级容易和干净的方法可以动态地使用PHP生成JavaScript,您通常会在此处或那里找到丑陋的代码。

That said, to generate forms dynamically seems a job made for a Form Library. 也就是说,动态生成表单似乎是表单库的工作。 I would imagine most frameworks provide something along that lines as it's a pretty common task. 我想大多数框架都会提供类似的功能,因为这是一项非常常见的任务。 You didn't state which Framework you are using, and if you aren't using one my first suggestion would be to get your hands on one. 您没有说明要使用哪个Framework,如果不使用它,我的第一个建议就是亲身体验。

I have worked with Zend_Form myself and they work quite good, with a little bit of coding you can get yourself the tool you are looking for. 我自己曾与Zend_Form一起工作,它们工作得很好,只需编写一些代码,您便可以为自己找到所需的工具。 For example, Zend_Form used to create a simple autocomplete using Dijit's forms 例如,Zend_Form用于使用Dijit的表单创建简单的自动完成功能

$element = new Zend_Dojo_Form_Element_FilteringSelect($variable);
$element->setStoreId($variable . "_store");
$element->setStoreType("dojox.data.QueryReadStore");
$element->setStoreParams(array("url" => "url/autocomplete-key-value?company=1"));

This is not a nested element, but you can get the idea of how they work in general. 这不是嵌套元素,但是您可以大致了解它们的工作原理。 If you post which framework you are using you can probably get a better answer. 如果发布您正在使用的框架,则可能会得到更好的答案。

I hope it helps!. 希望对您有所帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM