javascript获取div内的所有输入,包括select和textarea [英] javascript get all inputs inside div including select and textarea

查看:587
本文介绍了javascript获取div内的所有输入,包括select和textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出如何在div中获取所有输入元素(包括select和textarea)并将其传递给编辑器,到目前为止,我已经弄清楚了输入内容,但是我只剩下其余的内容了.

I have been trying to figure out how to get all the input elements inside a div including select and textarea and pass them to editor, so far i figured out with input but i am just stuck with the rest.

这是到目前为止的代码

function InsertShortcode(elem) {
        var shortcodeName = elem.parentElement.id;
        var inputs = document.getElementById(shortcodeName).getElementsByTagName('input'), i=0, e;
        var inputs_val = '[' + shortcodeName;
        while(e=inputs[i++]){
            if(e.id){
                inputs_val += ' ' + e.id + '="' + e.value + '"';
            }
        }
        inputs_val += ']';

        window.send_to_editor(inputs_val);
    }

通过这种方式,我可以在提交按钮所在的div中抓取所有输入,但是我仍然不确定如何抓取textarea或选择输入.

By this i am able to grab all the inputs inside a div where submit button is but still i am not sure how to grab textarea or select inputs.

问题是我必须使其动态.我将有许多短代码",每个短代码"都在按钮所在的它自己的div中.但是每个人都有自己无法控制的输入,因此我需要全部抓住它们并将值发送给编辑器.这是代码示例.

The problem is that i have to make it dynamic. I will have many "shortcodes" and each will be in it's own div where the button is. But each will have it's own inputs which i can't control so i need to grab them all and send values to editor. Here's example of the code.

    <div class="output-shortcodes">
        <?php foreach( $theme_shortcodes as $key => $name ) { ?>
            <div id="<?php echo $key ?>">
                <p>
                    <h2><?php echo $name ?></h2>
                </p>
                <?php $form = $key . '_form';
                    if(function_exists($form)) {
                        $form(); // this is where the input fields are dynamically created on each shortcode.
                    }
                ?>
                <button class="button-primary" onclick="InsertShortcode(this)">Insert shortcode</button>
            </div>
        <?php } ?>
    </div>

推荐答案

使用jQuery和:input伪选择器:

Use jQuery and the :input pseudo selector:

$('.output-shortcodes').find(':input');

就这么简单.

https://api.jquery.com/input-selector/

或将其包装在<form>中,然后可以使用:

Or wrap it in a <form>, then you can use:

document.getElementById("outputForm").elements...

https://developer.mozilla.org/zh- US/docs/Web/API/HTMLFormElement/elements

这篇关于javascript获取div内的所有输入,包括select和textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆