从html + jquery中的输入字段访问值

时间:2016-01-28 21:05:03

标签: javascript jquery html

这是我的fiddle

我正在寻找从html中的inpt形式获取值。例如,我想获得在class FileSystem(): def __init__(self,filePath=None): self.children = [] if filePath != None: try: self.name, child = filePath.split("/", 1) self.children.append(FileSystem(child)) except (ValueError): self.name = filePath def addChild(self, filePath): try: thisLevel, nextLevel = filePath.split("/", 1) try: if thisLevel == self.name: thisLevel, nextLevel = nextLevel.split("/", 1) except (ValueError): self.children.append(FileSystem(nextLevel)) return for child in self.children: if thisLevel == child.name: child.addChild(nextLevel) return self.children.append(FileSystem(nextLevel)) except (ValueError): self.children.append(FileSystem(filePath)) def getChildren(self): return self.children def printAllChildren(self, depth = -1): depth += 1 print "\t"*depth + "Name: "+ self.name if len(self.children) > 0: print "\t"*depth +"{ Children:" for child in self.children: child.printAllChildren(depth) print "\t"*depth + "}" def makeDict(self): if len(self.children) > 0: dictionary = {self.name:[]} for child in self.children: dictionary[self.name].append(child.makeDict()) return dictionary else: return self.name records = ["base/images/graphs/one.png", "base/images/tikz/two.png", "base/refs/images/three.png", "base/one.txt", "base/chapters/two.txt"] myFiles = FileSystem(records[0]) for record in records[1:]: myFiles.addChild(record) print myFiles.makeDict() 输入表单中输入的值。

Cat name:

我正在思考类似下面的事情:

 <div id="admin-form">
        <form>
            Cat name:<input id="admin-cat-name" type="text" name="cat-name" placeholder="10" value="10"><br>
            Source:  <input id="admin-source" type="text" name="source"><br>
            Count:   <input id="admin-count" type="text" name="count">
            <button id="save-button" type="button">Save</button>
            <button id="cancel-button" type="button">Cancel</button>
        </form>
    </div>

但我无法从输入表单中获得值。任何人都可以建议我这样做吗?如果我以正确的方式解决这个问题?

2 个答案:

答案 0 :(得分:3)

您实际上在寻找.val()。使用以下代码:

var form2 = $('form #admin-cat-name').val();

来自docs

  

获取匹配元素集中第一个元素的当前值或设置每个匹配元素的值。

答案 1 :(得分:1)

您可以使用此$("#admin-cat-name").val()